﻿using System.Windows.Forms;
using Foreglance.MVP.App.Persons;
using Foreglance.MVP;
using Foreglance.MVP.Delegates;
using Foreglance.MVP.Views;

namespace Foreglance.MVP.App
{
    public class AppController
    {
        const int MenuItemWidth = 100;
        public event ActionEventHandler OnExitApp;

        public AppController(MenuStrip mainMenu, StatusStrip statusStrip)
        {
            mainMenu.Items.AddRange(new ToolStripItem[]
                                        {
                                            AddFileMainMenuItem(),
                                            AddEntitiesMainMenuItem(),
                                        });
        }

        private static ToolStripItem AddEntitiesMainMenuItem()
        {
            return AddMainMenuItem("Entities", AddEntityMainMenuItem<PersonMediator>("Person"));
        }

        private ToolStripMenuItem AddFileMainMenuItem()
        {
            return AddMainMenuItem("File", AddFileExitMainMenuItem());
        }

        private static ToolStripMenuItem AddMainMenuItem(string text, params ToolStripMenuItem[] menuSubItems)
        {
            var fileMainMenuItem = new ToolStripMenuItem { Text = text, Width = MenuItemWidth };
            fileMainMenuItem.DropDownItems.AddRange(menuSubItems);
            return fileMainMenuItem;
        }

        private ToolStripMenuItem AddFileExitMainMenuItem()
        {
            var menuItem = new ToolStripMenuItem {Text = "Exit", Width = MenuItemWidth};
            menuItem.Click += (s, e) => OnExitApp();
            return menuItem;
        }

        private static ToolStripMenuItem AddEntityMainMenuItem<TMediator>(string text)
            where TMediator: IMediator, new()
        {
            var menuItem = new ToolStripMenuItem(){Text = text, Width = MenuItemWidth};
            menuItem.Click += (s, e) => new TMediator().Activate();
            return menuItem;
        }
    }
}